package com.ywq.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ywq.app.biz.ArtAuctionBiz;
import com.ywq.app.domain.ArtOrders;
import com.ywq.common.exception.AppBusinessException;

public class BidThread extends Thread{
	//下单biz
	private ArtAuctionBiz artAuctionBiz;
		
	public ArtAuctionBiz getArtAuctionBiz() {
			return artAuctionBiz;
		}
		public void setArtAuctionBiz(ArtAuctionBiz artAuctionBiz) {
			this.artAuctionBiz = artAuctionBiz;
		}
	/**
	 * 单线程 1、设置工作内容 2、自我唤醒功能 3、线程关闭
	 */
 		private boolean shutdown = false;
		// 已经竞拍的作品List（未到五分钟）
		private List<Map<String,Object>> bidList = new ArrayList<Map<String,Object>>();
		//新增竞拍作品
		public void setBidArt(Map<String,Object> bidArt)
		{
				//作品id
				String id=(String) bidArt.get("id");
				System.out.println("____________________________________________________________"+id);
				if(bidList.size() < 1){
					System.out.println("____________________________________________________________");
					try {
						bidList.add(bidArt);
						artAuctionBiz.lockAccount(bidArt);
					} catch (AppBusinessException e) {
						e.printStackTrace();
					}
				}else{
					System.out.println(">>>>>>>>>>>1：****************************************************************");
					for( int m=bidList.size()-1;m>=0;m--){
						System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+bidList.size());
						//如果已存在则移除
						if(id.equals(bidList.get(m).get("id"))){
							//解锁账户
							try {
								System.out.println("解锁1：—*******"+bidList.get(m).get("memberId"));
								artAuctionBiz.unLockAccount(bidList.get(m));
								System.out.println("解锁2：—*"+m+"******"+bidList.get(m).get("memberId"));
								bidList.remove(m);
								System.out.println("解锁3：—*"+m+"******");
								System.out.println("枷锁1：—*"+m+"******");
								artAuctionBiz.lockAccount(bidArt);
								//bidList.add(bidArt);
								System.out.println("枷锁2：—*"+m+"******");
								System.out.println("解锁4：—*"+m+"******");
							} catch (AppBusinessException e) {
								e.printStackTrace();
							}
						}
					}
					try {
						bidList.add(bidArt);
						artAuctionBiz.lockAccount(bidArt);
					} catch (AppBusinessException e) {
						e.printStackTrace();
					}
			}
		}
		public void run()
		{
			while (!shutdown)
			{
					if (bidList.size() > 0)
					{
						//System.out.println("数字1：—*******"+bidList.size());
						try {
								for(int n=0;n< bidList.size();n++){
										//System.out.println("数字2：—*******"+n);
										String bidTime=(String)bidList.get(n).get("bidTime");
										
										SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
										Date bid = f.parse(bidTime);
										long longBid = bid.getTime();
										long longNow = new Date().getTime();
										long overTime =(longNow-longBid)/(1000*60);
										if(overTime>=5){
											Map<String,Object> biaSuccess = bidList.remove(n);
											System.out.println("价格：—*******"+biaSuccess.get("bidPrice"));
											//下单
											String id =(String)biaSuccess.get("id");
											ArtOrders artOrders =new ArtOrders();
											artOrders.setTypeId(id);
											artOrders.setMemberId((String)biaSuccess.get("memberId"));
											artOrders.setSucPrice(Float.valueOf((String)biaSuccess.get("bidPrice")));
											try {
												artAuctionBiz.insertArtOrders(artOrders);
											} catch (AppBusinessException e) {
												e.printStackTrace();
											}
										}
								}
						} catch (ParseException e) {
							e.printStackTrace();
						}
					}
			}
		}
}
